package com.szpt.mo.bhubackground.service.impl;

import com.szpt.mo.bhubackground.entity.TbUser;
import com.szpt.mo.bhubackground.mapper.TbUserMapper;
import com.szpt.mo.bhubackground.service.ITbUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.szpt.mo.bhubackground.util.config.Db;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;

/**
 * @Package: com.szpt.mo.bhubackground.service.impl
 * @Description: <服务实现类>
 * @Author: mo
 * @CreateDate: 2022-06-05
 * @UpdateUser: mo
 * @UpdateDate: 2022-06-05
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser> implements ITbUserService {
//
//    @Autowired
//    Db db;

    @Override
    public TbUser login1(String username, String password) {
        Db db = new Db();
        String sql = "select * from tb_user where mail = ? and password = ?;";
        ResultSet select = db.select(sql, "2697861661@qq.com", "17777777777");

        try {
            select.next();
            TbUser user = new TbUser();
            user.setId(select.getLong("id"));
            user.setRole(select.getString("role"));
            user.setName(select.getString("name"));
            user.setMail(select.getString("mail"));
            user.setPassword(select.getString("password"));
            user.setPhone(select.getString("phone"));
            user.setAvatar(select.getString("avatar"));
            Time create_time = select.getTime("create_time");
            user.setCreateTime( Instant.ofEpochMilli(create_time.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime());
            Time update_time = select.getTime("update_time");
            user.setUpdateTime(Instant.ofEpochMilli(update_time.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime());
            select.close();
            db.close();

            return user;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }
}

